create or alter procedure PKTOLISTE (
    VON char(8),
    BIS char(8),
    PERIODE integer,
    MITOPO integer,
    OHNEERKONTROLLE integer,
    OHNEGESPERRT integer,
    MITHIST integer,
    LOESCHPERIODE integer,
    OHNEANZBH integer,
    OHNEARKONTROLLE integer,
    SORTORDER integer)
returns (
    LFDNR integer,
    TYP char(1),
    NR char(8),
    BEZ char(200),
    JAHRSALDO double precision,
    VORTRAG double precision,
    JAHRUMSATZ double precision,
    MONATUMSATZ double precision,
    UIDNR char(40),
    DEBITORKREDITOR char(1),
    NAME1 char(40),
    NAME2 char(40),
    NAME3 char(40),
    STRASSE char(60),
    ORT char(40),
    TEL char(40),
    FAX char(25),
    FORDKTONR char(8),
    OPOID integer,
    BELEGNR char(15),
    GELOESCHT char(1),
    RESTBETRAG double precision,
    EXTBELEGNR char(40),
    BELEGDATUM date,
    FTAGE integer,
    MAHNSTUFE integer,
    BRUTTO double precision,
    TEXT char(200),
    STEUERSATZID integer,
    VU char(1),
    MAHNZAHLSTOP integer,
    BVNR char(10),
    BVSYMBOL char(10),
    OPERIODE integer,
    SELKRIT char(26),
    KREDITLIMIT double precision,
    OPOKOST char(8),
    KONTOKORRENT char(8),
    VALUTADATUM date,
    ZAHLDATUMPLAN date,
    BEWERTUNG double precision,
    JASOLL float,
    JAHABEN float,
    ANZBUCHSCHAB varchar(20),
    TELHANDY varchar(25),
    EMAIL varchar(64))
AS
declare variable JAHR float;
declare variable MONAT float;
declare variable IJAHR integer;
declare variable ANFANGSMONAT integer;
declare variable GJMONAT integer;
declare variable GJMONAT1 integer;
declare variable GJMONAT2 integer;
declare variable GJJAHR integer;
declare variable PERIODE0 integer;
declare variable PERIODE1 integer;
declare variable PERIODE2 integer;
declare variable PERIODE3 integer;
declare variable PERIODE4 integer;
declare variable PERIODE5 integer;
declare variable PERIODE6 integer;
declare variable PERIODE7 integer;
declare variable PERIODE8 integer;
declare variable PERIODE9 integer;
declare variable PERIODE10 integer;
declare variable PERIODE11 integer;
declare variable PERIODE12 integer;
declare variable PERIODE13 integer;
declare variable DSALDO double precision;
declare variable DUMSATZ double precision;
declare variable ABBRUCH smallint;
declare variable VPERIODE integer;
declare variable XPERIODE integer;
declare variable ADRID integer;
declare variable SPERRE char(1);
declare variable DRUCK integer;
declare variable OK1 integer; /* Abfrage ER Kontrolle */
declare variable OK2 integer; /* Abfrage AnzBH */
declare variable OK3 integer;
declare variable DSOLL float;
declare variable DHABEN float;
begin
  /* Version 12 vom 16.02.2016 */
  lfdnr=0;
  jahr=:PERIODE / 100;
  ijahr=jahr;
  monat=:PERIODE-ijahr*100;
  /* Anfangsmonat */
  select anfangsmonat from fibufist where id=:periode into :ANFANGSMONAT;
  if (anfangsmonat is null) then begin
    select max(anfangsmonat) from fibufist into :anfangsmonat;
  end
  /* Berechnung der Geschaeftsjahres-Liste */
  gjmonat=0;
  gjmonat1=anfangsmonat;
  gjmonat2=monat;
  gjjahr=ijahr;
  if (gjmonat1=gjmonat2) then gjmonat=gjmonat1;
  if (gjmonat1>gjmonat2) then begin
    gjmonat=gjmonat1;
    gjjahr=gjjahr-1;
  end
  if (gjmonat1<gjmonat2) then gjmonat=gjmonat1;
  if (gjmonat2=0) then begin
    gjmonat=gjmonat1;
    gjjahr=ijahr;
  end
  if (gjmonat2=13) then begin
    gjmonat=gjmonat1;
    gjjahr=ijahr;
  end
  /* Periode 0 */
  periode0=gjjahr*100;
  /* Periode 13 */
  periode13=gjjahr*100+13;
  /* Periode 1 */
  periode1=gjjahr*100+gjmonat;
  /* Periode 2 */
  gjmonat=gjmonat+1;
  if (gjmonat>12) then begin
    gjmonat=1;
    gjjahr=gjjahr+1;
  end
  periode2=gjjahr*100+gjmonat;
  /* Periode 3 */
  gjmonat=gjmonat+1;
  if (gjmonat>12) then begin
    gjmonat=1;
    gjjahr=gjjahr+1;
  end
  periode3=gjjahr*100+gjmonat;
  /* Periode 4 */
  gjmonat=gjmonat+1;
  if (gjmonat>12) then begin
    gjmonat=1;
    gjjahr=gjjahr+1;
  end
  periode4=gjjahr*100+gjmonat;
  /* Periode 5 */
  gjmonat=gjmonat+1;
  if (gjmonat>12) then begin
    gjmonat=1;
    gjjahr=gjjahr+1;
  end
  periode5=gjjahr*100+gjmonat;
  /* Periode 6 */
  gjmonat=gjmonat+1;
  if (gjmonat>12) then begin
    gjmonat=1;
    gjjahr=gjjahr+1;
  end
  periode6=gjjahr*100+gjmonat;
  /* Periode 7 */
  gjmonat=gjmonat+1;
  if (gjmonat>12) then begin
    gjmonat=1;
    gjjahr=gjjahr+1;
  end
  periode7=gjjahr*100+gjmonat;
  /* Periode 8 */
  gjmonat=gjmonat+1;
  if (gjmonat>12) then begin
    gjmonat=1;
    gjjahr=gjjahr+1;
  end
  periode8=gjjahr*100+gjmonat;
  /* Periode 9 */
  gjmonat=gjmonat+1;
  if (gjmonat>12) then begin
    gjmonat=1;
    gjjahr=gjjahr+1;
  end
  periode9=gjjahr*100+gjmonat;
  /* Periode 10 */
  gjmonat=gjmonat+1;
  if (gjmonat>12) then begin
    gjmonat=1;
    gjjahr=gjjahr+1;
  end
  periode10=gjjahr*100+gjmonat;
  /* Periode 11 */
  gjmonat=gjmonat+1;
  if (gjmonat>12) then begin
    gjmonat=1;
    gjjahr=gjjahr+1;
  end
  periode11=gjjahr*100+gjmonat;
  /* Periode 12 */
  gjmonat=gjmonat+1;
  if (gjmonat>12) then begin
    gjmonat=1;
    gjjahr=gjjahr+1;
  end
  periode12=gjjahr*100+gjmonat;
  /* Schleife durch die Personenkonten */
  for select nr,uidnr,debitorkreditor,adrid,fordktonr,selkrit,kreditlimit
  from pkto where nr>=:VON and nr<=:BIS
  into :nr,:uidnr,:debitorkreditor,:adrid,:fordktonr,:selkrit,:kreditlimit
  do begin
  /* Bezeichnung */
  select bez from kto where kto.nr=:nr into :BEZ;
  /* Adress-Felder */
  name1=NULL;
  name2=NULL;
  name3=NULL;
  strasse=NULL;
  ort=NULL;
  tel=NULL;
  fax=NULL;
  telhandy=NULL;
  email=NULL;
  select name1,name2,name3,strasse,ort,tel,fax,telhandy,email from adr
  where id=:adrid
  into :name1,:name2,:name3,:strasse,:ort,:tel,:fax,:telhandy,:email;
  /* Vortragssaldo */
  vortrag=0.00;
  jasoll=0.00;
  jahaben=0.00;
  if (anfangsmonat<>0) then begin
    select saldo,soll,haben from mowert where nr=:nr and periode=:PERIODE0
      into :vortrag,:jasoll,:jahaben;
  end else begin
    select sum(saldo),sum(soll),sum(haben) from mowert,fibufist where mowert.periode=fibufist.id
    and periodensteuerung=256 and nr=:nr into :vortrag,:jasoll,:jahaben;
  end
  /* Jahres-Saldo, Jahres-Umsatz, Jahres-Soll, Jahres-Haben */
  if (anfangsmonat<>0) then begin
    jahrsaldo=vortrag;
    if (jahrsaldo is null) then jahrsaldo=0.00;
    JahrUmsatz=0.00;
    abbruch=0;
    /* Periode 1 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE1
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
      if (periode1=:Periode) then abbruch=1;
    end
    /* Periode 2 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE2
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
      if (periode2=:Periode) then abbruch=1;
    end
    /* Periode 3 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE3
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
      if (periode3=:Periode) then abbruch=1;
    end
    /* Periode 4 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE4
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
      if (periode4=:Periode) then abbruch=1;
    end
    /* Periode 5 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE5
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
      if (periode5=:Periode) then abbruch=1;
    end
    /* Periode 6 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE6
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
      if (periode6=:Periode) then abbruch=1;
    end
    /* Periode 7 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE7
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
      if (periode7=:Periode) then abbruch=1;
    end
    /* Periode 8 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE8
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
      if (periode8=:Periode) then abbruch=1;
    end
    /* Periode 9 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE9
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
      if (periode9=:Periode) then abbruch=1;
    end
    /* Periode 10 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE10
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
      if (periode10=:Periode) then abbruch=1;
    end
    /* Periode 11 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE11
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
      if (periode11=:Periode) then abbruch=1;
    end
    /* Periode 12 */
    if (abbruch=0) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE12
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
    end
    /* Periode 13 */
    if (monat=13) then begin
      dsaldo=0;
      dumsatz=0;
      dsoll=0.00;
      dhaben=0.00;
      select saldo,umsatz,soll,haben from mowert where nr=:nr and periode=:PERIODE13
        into :DSALDO,:DUMSATZ,:DSOLL,:DHABEN;
      jahrsaldo=jahrsaldo + dsaldo;
      jahrumsatz=jahrumsatz + dumsatz;
      jasoll=jasoll+dsoll;
      jahaben=jahaben+dhaben;
    end
  end else begin
    jahrsaldo=vortrag;
    dsaldo=0;
    dsoll=0.00;
    dhaben=0.00;
    if (jahrsaldo is null) then jahrsaldo=0.00;
    select sum(SALDO),sum(SOLL),sum(HABEN) from mowert,fibufist where
    mowert.periode=fibufist.id and mowert.periode<=:PERIODE
    and periodensteuerung=0 and nr=:nr into dsaldo,dsoll,dhaben;
    if (dsaldo is null) then dsaldo=0.00;
    jahrsaldo=jahrsaldo+dsaldo;
    select sum(UMSATZ) from mowert where
    nr=:nr and PERIODE<=:periode into :JahrUmsatz;
  end
  /* Monats-Umsatz */
  MonatUmsatz=0.00;
  select umsatz from mowert where nr=:nr and periode=:periode into :MonatUmsatz;
  Typ="P";
  Opoid=null;
  Belegnr=null;
  /* Gesperrt ? */
  Druck=0;
  if (ohnegesperrt=1) then begin
    select gesperrt from kto where nr=:nr into :sperre;
    if ((sperre=0) or (sperre is null)) then begin
      lfdnr=lfdnr+1;
      suspend;
      Druck=1;
    end
  end else begin
    Druck=1;
    lfdnr=lfdnr+1;
    suspend;
  end
  /* Offene Posten */
  if ((mitopo=1) and (druck=1)) then begin
    VPeriode=periode0+100;
    if (monat=13) then begin
      XPeriode=Periode12;
    end else begin
      XPeriode=Periode;
    end
    if ((anfangsmonat=1) and (monat=13)) then begin
      XPeriode=Periode13;
    end
    /* OPO geordnet nach OPO-ID */
    if (sortorder=1) then begin
      for select id,beleg,geloescht,restbetrag,extbelegnr,belegdatum,ftage,
      mahnstufe,brutto,text,steuersatzid,vu,mahnzahlstop,bvnr,bvsymbol,periode,kost,valutadatum,zahldatumplan,bewertung
      from opost where kontonummer=:nr and periode<=:XPeriode
      and periode<>:VPeriode and ((loeschperiode=0) or (loeschperiode is null) or ((loeschperiode>:loeschperiode) or (geloescht<>"1"))) order by ID
      into :opoid,:belegnr,:geloescht,:restbetrag,:extbelegnr,:belegdatum,:ftage,
      :mahnstufe,:brutto,:text,:steuersatzid,:vu,:mahnzahlstop,:bvnr,:bvsymbol,:operiode,:OPOKOST,:valutadatum,:zahldatumplan,:bewertung
      do begin
        anzbuchschab=NULL;
        Execute Procedure PktoListeSub(BVNR,BVSYMBOL,MONAT,PERIODE13,OPERIODE,MITHIST,OPOID,PERIODE,PERIODE12,RESTBETRAG,OHNEERKONTROLLE,OHNEANZBH,OHNEARKONTROLLE)
        returning_values :DRUCK,:KONTOKORRENT,:OPERIODE,:TYP,:RESTBETRAG,OK1,OK2,OK3,anzbuchschab;
        /* Filtern? */
        if ((OK1=1) and (OK2=1) and (OK3=1)) then Begin
          lfdnr=lfdnr+1;
          suspend;
        End
      end
    end
    /* OPO geordnet nach Belegnummer */
    if (sortorder=2) then begin
      for select id,beleg,geloescht,restbetrag,extbelegnr,belegdatum,ftage,
      mahnstufe,brutto,text,steuersatzid,vu,mahnzahlstop,bvnr,bvsymbol,periode,kost,valutadatum,zahldatumplan,bewertung
      from opost where kontonummer=:nr and periode<=:XPeriode
      and periode<>:VPeriode and ((loeschperiode=0) or (loeschperiode is null) or ((loeschperiode>:loeschperiode) or (geloescht<>"1"))) order by BELEG
      into :opoid,:belegnr,:geloescht,:restbetrag,:extbelegnr,:belegdatum,:ftage,
      :mahnstufe,:brutto,:text,:steuersatzid,:vu,:mahnzahlstop,:bvnr,:bvsymbol,:operiode,:OPOKOST,:valutadatum,:zahldatumplan,:bewertung
      do begin
        anzbuchschab=NULL;
        Execute Procedure PktoListeSub(BVNR,BVSYMBOL,MONAT,PERIODE13,OPERIODE,MITHIST,OPOID,PERIODE,PERIODE12,RESTBETRAG,OHNEERKONTROLLE,OHNEANZBH,OHNEARKONTROLLE)
        returning_values :DRUCK,:KONTOKORRENT,:OPERIODE,:TYP,:RESTBETRAG,OK1,OK2,OK3,anzbuchschab;
        /* Filtern? */
        if ((OK1=1) and (OK2=1) and (OK3=1)) then Begin
          lfdnr=lfdnr+1;
          suspend;
        End
      end
    end
    /* OPO geordnet nach BV-NR */
    if (sortorder=3) then begin
      for select id,beleg,geloescht,restbetrag,extbelegnr,belegdatum,ftage,
      mahnstufe,brutto,text,steuersatzid,vu,mahnzahlstop,bvnr,bvsymbol,periode,kost,valutadatum,zahldatumplan,bewertung
      from opost where kontonummer=:nr and periode<=:XPeriode
      and periode<>:VPeriode and ((loeschperiode=0) or (loeschperiode is null) or ((loeschperiode>:loeschperiode) or (geloescht<>"1"))) order by BVNR
      into :opoid,:belegnr,:geloescht,:restbetrag,:extbelegnr,:belegdatum,:ftage,
      :mahnstufe,:brutto,:text,:steuersatzid,:vu,:mahnzahlstop,:bvnr,:bvsymbol,:operiode,:OPOKOST,:valutadatum,:zahldatumplan,:bewertung
      do begin
        anzbuchschab=NULL;
        Execute Procedure PktoListeSub(BVNR,BVSYMBOL,MONAT,PERIODE13,OPERIODE,MITHIST,OPOID,PERIODE,PERIODE12,RESTBETRAG,OHNEERKONTROLLE,OHNEANZBH,OHNEARKONTROLLE)
        returning_values :DRUCK,:KONTOKORRENT,:OPERIODE,:TYP,:RESTBETRAG,OK1,OK2,OK3,anzbuchschab;
        /* Filtern? */
        if ((OK1=1) and (OK2=1) and (OK3=1)) then Begin
          lfdnr=lfdnr+1;
          suspend;
        End
      end
    end
    /* OPO geordnet nach Belegdatum */
    if (sortorder=4) then begin
      for select id,beleg,geloescht,restbetrag,extbelegnr,belegdatum,ftage,
      mahnstufe,brutto,text,steuersatzid,vu,mahnzahlstop,bvnr,bvsymbol,periode,kost,valutadatum,zahldatumplan,bewertung
      from opost where kontonummer=:nr and periode<=:XPeriode
      and periode<>:VPeriode and ((loeschperiode=0) or (loeschperiode is null) or ((loeschperiode>:loeschperiode) or (geloescht<>"1"))) order by BELEGDATUM,BELEG
      into :opoid,:belegnr,:geloescht,:restbetrag,:extbelegnr,:belegdatum,:ftage,
      :mahnstufe,:brutto,:text,:steuersatzid,:vu,:mahnzahlstop,:bvnr,:bvsymbol,:operiode,:OPOKOST,:valutadatum,:zahldatumplan,:bewertung
      do begin
        anzbuchschab=NULL;
        Execute Procedure PktoListeSub(BVNR,BVSYMBOL,MONAT,PERIODE13,OPERIODE,MITHIST,OPOID,PERIODE,PERIODE12,RESTBETRAG,OHNEERKONTROLLE,OHNEANZBH,OHNEARKONTROLLE)
        returning_values :DRUCK,:KONTOKORRENT,:OPERIODE,:TYP,:RESTBETRAG,OK1,OK2,OK3,anzbuchschab;
        /* Filtern? */
        if ((OK1=1) and (OK2=1) and (OK3=1)) then Begin
          lfdnr=lfdnr+1;
          suspend;
        End
      end
    end
    /* OPO geordnet nach Steuersatz */
    if (sortorder=5) then begin
      for select id,beleg,geloescht,restbetrag,extbelegnr,belegdatum,ftage,
      mahnstufe,brutto,text,steuersatzid,vu,mahnzahlstop,bvnr,bvsymbol,periode,kost,valutadatum,zahldatumplan,bewertung
      from opost where kontonummer=:nr and periode<=:XPeriode
      and periode<>:VPeriode and ((loeschperiode=0) or (loeschperiode is null) or ((loeschperiode>:loeschperiode) or (geloescht<>"1"))) order by STEUERSATZID
      into :opoid,:belegnr,:geloescht,:restbetrag,:extbelegnr,:belegdatum,:ftage,
      :mahnstufe,:brutto,:text,:steuersatzid,:vu,:mahnzahlstop,:bvnr,:bvsymbol,:operiode,:OPOKOST,:valutadatum,:zahldatumplan,:bewertung
      do begin
        anzbuchschab=NULL;
        Execute Procedure PktoListeSub(BVNR,BVSYMBOL,MONAT,PERIODE13,OPERIODE,MITHIST,OPOID,PERIODE,PERIODE12,RESTBETRAG,OHNEERKONTROLLE,OHNEANZBH,OHNEARKONTROLLE)
        returning_values :DRUCK,:KONTOKORRENT,:OPERIODE,:TYP,:RESTBETRAG,OK1,OK2,OK3,anzbuchschab;
        /* Filtern? */
        if ((OK1=1) and (OK2=1) and (OK3=1)) then Begin
          lfdnr=lfdnr+1;
          suspend;
        End
      end
    end
    /* OPO geordnet nach Kostenstelle*/
    if (sortorder=6) then begin
      for select id,beleg,geloescht,restbetrag,extbelegnr,belegdatum,ftage,
      mahnstufe,brutto,text,steuersatzid,vu,mahnzahlstop,bvnr,bvsymbol,periode,kost,valutadatum,zahldatumplan,bewertung
      from opost where kontonummer=:nr and periode<=:XPeriode
      and periode<>:VPeriode and ((loeschperiode=0) or (loeschperiode is null) or ((loeschperiode>:loeschperiode) or (geloescht<>"1"))) order by KOST
      into :opoid,:belegnr,:geloescht,:restbetrag,:extbelegnr,:belegdatum,:ftage,
      :mahnstufe,:brutto,:text,:steuersatzid,:vu,:mahnzahlstop,:bvnr,:bvsymbol,:operiode,:OPOKOST,:valutadatum,:zahldatumplan,:bewertung
      do begin
        anzbuchschab=NULL;
        Execute Procedure PktoListeSub(BVNR,BVSYMBOL,MONAT,PERIODE13,OPERIODE,MITHIST,OPOID,PERIODE,PERIODE12,RESTBETRAG,OHNEERKONTROLLE,OHNEANZBH,OHNEARKONTROLLE)
        returning_values :DRUCK,:KONTOKORRENT,:OPERIODE,:TYP,:RESTBETRAG,OK1,OK2,OK3,anzbuchschab;
        /* Filtern? */
        if ((OK1=1) and (OK2=1) and (OK3=1)) then Begin
          lfdnr=lfdnr+1;
          suspend;
        End
      end
    end
    /* OPO geordnet nach Mahnstufe */
    if (sortorder=7) then begin
      for select id,beleg,geloescht,restbetrag,extbelegnr,belegdatum,ftage,
      mahnstufe,brutto,text,steuersatzid,vu,mahnzahlstop,bvnr,bvsymbol,periode,kost,valutadatum,zahldatumplan,bewertung
      from opost where kontonummer=:nr and periode<=:XPeriode
      and periode<>:VPeriode and ((loeschperiode=0) or (loeschperiode is null) or ((loeschperiode>:loeschperiode) or (geloescht<>"1"))) order by MAHNSTUFE,BELEGDATUM
      into :opoid,:belegnr,:geloescht,:restbetrag,:extbelegnr,:belegdatum,:ftage,
      :mahnstufe,:brutto,:text,:steuersatzid,:vu,:mahnzahlstop,:bvnr,:bvsymbol,:operiode,:OPOKOST,:valutadatum,:zahldatumplan,:bewertung
      do begin
        anzbuchschab=NULL;
        Execute Procedure PktoListeSub(BVNR,BVSYMBOL,MONAT,PERIODE13,OPERIODE,MITHIST,OPOID,PERIODE,PERIODE12,RESTBETRAG,OHNEERKONTROLLE,OHNEANZBH,OHNEARKONTROLLE)
        returning_values :DRUCK,:KONTOKORRENT,:OPERIODE,:TYP,:RESTBETRAG,OK1,OK2,OK3,anzbuchschab;
        /* Filtern? */
        if ((OK1=1) and (OK2=1) and (OK3=1)) then Begin
          lfdnr=lfdnr+1;
          suspend;
        End
      end
    end
    /* Ende ausgeben */
    Typ="E";
    opoid=null;
    Belegnr=null;
    lfdnr=lfdnr+1;
    suspend;
    end
  end
end^
